package com.desire.homework;

import java.util.Stack;

public class _24_字符逆序 {
    public static void main(String[] args) {
        String str = "ABC123ABC";
        StringBuilder stringBuilder = new StringBuilder(str);
        stringBuilder.reverse();
        String newStr = stringBuilder.toString();
        System.out.println("反转前：" + str);
        System.out.println("反转后：" + newStr);

        System.out.println("*".repeat(20));

        // 利用 栈 特性,  先进后出
        String s = "asdf1234";

        char[] chars = s.toCharArray();
        Stack<Character> stack = new Stack<>();
        StringBuilder stringBuilder1 = new StringBuilder();
        // 压栈  (将字符一个一个压进  栈)
        for (char aChar : chars) {
            stack.push(aChar);
        }
        // 出栈  ( 将字符 一个一个pop出去 并追加到 字符串数组中)
        for (char aChar : chars) {
            stringBuilder1.append(stack.pop());
        }

        System.out.println("反转前：" + s);
        System.out.println("反转后：" + stringBuilder1);

    }
}
